package org.n.shop.service.impl;

import lombok.AllArgsConstructor;
import org.n.shop.domain.Do.SelectAllOrderDetailDo;
import org.n.shop.domain.Do.SelectLastOrderDetailDo;
import org.n.shop.domain.Do.SelectOrderDetailByIdDo;
import org.n.shop.domain.vo.ListVo;
import org.n.shop.mapper.OrderDetailMapper;
import org.n.shop.result.RestResult;
import org.n.shop.result.RestResultBuilder;
import org.n.shop.service.OrderDetailService;
import org.n.shop.util.MinioUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static org.n.shop.config.RedisConfig.PREFIX_USER;

@Service
@AllArgsConstructor
public class OrderDetailServiceImpl implements OrderDetailService {
    private OrderDetailMapper orderDetailMapper;
    private RedisTemplate redisTemplate;
    private MinioUtil minioUtil;

    @Override
    public RestResult last(String authorization) {
        int userId = (int) redisTemplate.opsForValue().get(PREFIX_USER + authorization);

        List<SelectLastOrderDetailDo> selectLastOrderDetailDos = orderDetailMapper.selectLastOrderDetail(userId);

        for (SelectLastOrderDetailDo selectLastOrderDetailDo : selectLastOrderDetailDos) {
            selectLastOrderDetailDo.setImage(minioUtil.getObject(selectLastOrderDetailDo.getImage()));
        }

        return new RestResultBuilder().success(
                ListVo.builder().list(selectLastOrderDetailDos).build()
        );
    }

    @Override
    public RestResult all(String authorization, int timeType, int page, int pageSize, int logisticsState, String key) {
        int userId = (int) redisTemplate.opsForValue().get(PREFIX_USER + authorization);

        List<SelectAllOrderDetailDo> selectAllOrderDetailDos =
                orderDetailMapper.selectAllOrderDetail(userId, timeType, page, pageSize, logisticsState, key);

        for (SelectAllOrderDetailDo selectAllOrderDetailDo : selectAllOrderDetailDos) {
            if (selectAllOrderDetailDo.getLogisticsState() == 0) {
                selectAllOrderDetailDo.setTime(selectAllOrderDetailDo.getTime().plusMinutes(30));
            }
            selectAllOrderDetailDo.setImage(minioUtil.getObject(selectAllOrderDetailDo.getImage()));
        }

        return new RestResultBuilder().success(
                ListVo.builder()
                        .list(selectAllOrderDetailDos)
                        .total(orderDetailMapper.totalAllOrderDetail(userId, timeType, logisticsState, key))
                        .build()
        );
    }

    @Override
    public RestResult del(int id) {
        if (orderDetailMapper.del(id) == 1) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail("删除失败");
    }

    @Override
    public RestResult id(int id) {
        SelectOrderDetailByIdDo selectOrderDetailByIdDo = orderDetailMapper.selectOrderDetailById(id);
        selectOrderDetailByIdDo.setImage(minioUtil.getObject(selectOrderDetailByIdDo.getImage()));
        return new RestResultBuilder().success(selectOrderDetailByIdDo);
    }
}
